package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.SleepInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdSleep;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdSleepMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdSleepService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.StringJoiner;

/**
 * <p>
 * 匹兹堡睡眠质量评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdSleepServiceImpl extends ServiceImpl<DtrdEntEvaluationRdSleepMapper, DtrdEntEvaluationRdSleep> implements IDtrdEntEvaluationRdSleepService {

    @Override
    public SleepInfo saveSleepInfo(SleepInfo sleepInfo) {
        int totalScore = 0;
        int tmpScore = 0;
        int a_sleepQuality = 0;
        int b_asleepTime = 0;
        int c_sleepTime = 0;
        int d_sleepEfficiency = 0;
        int e_sleepDisorder = 0;
        int f_sodiumAmytal = 0;
        int g_daytimeDysfunction = 0;
        int asleepTime = 0;
        int bedTime = 0;
        int sleepEfficiency = 0;
        List<Integer> answerArr = sleepInfo.getInput();
        StringJoiner joiner = new StringJoiner(",");
        for (int i = 0; i < answerArr.size(); i++) {
            tmpScore = answerArr.get(i);
            joiner.add(tmpScore + "");
            if (15 == i + 1) {
                a_sleepQuality += tmpScore;
            }
            if (2 == i + 1 || 5 == i + 1) {
                b_asleepTime += tmpScore;
            }
            if (1 == i + 1) {
                bedTime += tmpScore;
            }
            if (4 == i + 1) {
                asleepTime += tmpScore;
                if (asleepTime <= 4)
                    c_sleepTime = 0;
                else if (asleepTime <= 6)
                    c_sleepTime = 1;
                else if (asleepTime <= 7)
                    c_sleepTime = 2;
                else if (asleepTime <= 11)
                    c_sleepTime = 3;
                sleepEfficiency = (12 - asleepTime) * 100 / (12 - bedTime);
                if (sleepEfficiency > 85)
                    d_sleepEfficiency = 0;
                else if (sleepEfficiency >= 75)
                    d_sleepEfficiency = 1;
                else if (sleepEfficiency >= 65)
                    d_sleepEfficiency = 2;
                else
                    d_sleepEfficiency = 3;
            }
            if (6 == i + 1 || 7 == i + 1 || 8 == i + 1 || 9 == i + 1 || 10 == i + 1 || 11 == i + 1 || 12 == i + 1 || 13 == i + 1 || 14 == i + 1) {
                e_sleepDisorder += tmpScore;
            }
            if (16 == i + 1) {
                f_sodiumAmytal += tmpScore;
            }
            if (17 == i + 1 || 18 == i + 1) {
                g_daytimeDysfunction += tmpScore;
            }
        }
        b_asleepTime = b_asleepTime / 2;
        e_sleepDisorder = e_sleepDisorder / 9;
        g_daytimeDysfunction = g_daytimeDysfunction / 2;
        totalScore = a_sleepQuality + b_asleepTime + c_sleepTime + d_sleepEfficiency + e_sleepDisorder + f_sodiumAmytal + g_daytimeDysfunction;
        String level = "";
        String output = "";
        if (totalScore <= 5) {
            level = "睡眠质量很好";
        } else if (totalScore <= 10) {
            level = "睡眠质量良好";
        } else if (totalScore <= 15) {
            level = "睡眠质量很一般";
        } else if (totalScore <= 21) {
            level = "睡眠质量较差";
        }
        DtrdEntEvaluationRdSleep po = new DtrdEntEvaluationRdSleep().init();
        po.setInput(joiner.toString());
        po.setTotal(totalScore);
        po.setLevel(level);
        po.setOutput(level);
        if (sleepInfo.getPatientId() != null) {
            po.setPatientId(sleepInfo.getPatientId());
            save(po);
        }
        return new SleepInfo().parseFromPo(po);
    }

    @Override
    public SleepInfo getSleepInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdSleep> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdSleep.class)
                .eq(DtrdEntEvaluationRdSleep::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdSleep::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdSleep sleep = this.getOne(queryWrapper);
        SleepInfo sleepInfo = new SleepInfo();
        if (sleep != null) {
            sleepInfo.parseFromPo(sleep);
        }
        return sleepInfo;
    }
}
